package com.bbcare.followup.plat.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.followup.plat.service.IGy3yService;


@Controller
@RequestMapping("/gy3y")
public class Gy3yController1 {
    private final static Logger logger = Logger.getLogger(Gy3yController1.class);

    @Autowired
    private IGy3yService gy3yService;

    /**
     * 查询体格3项的周增长。
     * @param request
     * @param arg
     * @return
     * @throws Exception
     */
    @PostMapping("/queryGrowthWeekly")
    @ResponseBody
    public ResultModel<Map<String, Object>> queryGrowthWeekly(HttpServletRequest request, @RequestBody String arg) throws Exception {
        logger.info("++++queryGrowthWeekly() arg:" + arg.toString());
        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }
        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }

        final String taskId  = jsonData.getString("taskId");
        if (StringUtils.isBlank(taskId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入队列taskId或值为空！"));
        }
        final String followupDate   = jsonData.getString("followupDate");
        if (StringUtils.isBlank(followupDate)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入队列followupDate或值为空！"));
        }

        Map<String,String> paraMap  = new HashMap<>();
        final String weight = jsonData.getString("weight");
        final String length = jsonData.getString("length");
        final String head   = jsonData.getString("head");
        if (StringUtils.isBlank(weight) && StringUtils.isBlank(length) && StringUtils.isBlank(head))    {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入体格数据！"));
        } else {
            if (StringUtils.isNotBlank(weight)) {
                paraMap.put("tz", weight);
            }
            if (StringUtils.isNotBlank(length)) {
                paraMap.put("sc", length);
            }
            if (StringUtils.isNotBlank(head)) {
                paraMap.put("tw", head);
            }
        }

        return new ResultModel(gy3yService.dealGrowthWeekly(followupDate, taskId, paraMap));
    }

    /**
     * 根据母亲手机号查询家庭成员信息。
     * @param request
     * @param arg
     * @return
     * @throws Exception
     */
    @PostMapping("/queryFamilyInfo")
    @ResponseBody
    public ResultModel<Map<String, Object>> queryFamilyInfo(HttpServletRequest request, @RequestBody String arg) throws Exception {
        logger.info("++++queryFamilyInfo() arg:" + arg.toString());
        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }
        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }

        final String queueId  = jsonData.getString("queueId");
        if (StringUtils.isBlank(queueId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入队列queueId或值为空！"));
        }
        final String phone  = jsonData.getString("phone");
        if (StringUtils.isBlank(phone)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入队列phone或值为空！"));
        }

        Map<String,Object> retMap   = gy3yService.queryParentInfo(phone, queueId);
        Map<String,Object> retMap2  = new HashMap<>();
        retMap2.put("values", retMap);

        return new ResultModel(retMap2);
    }
}
